package com.jian.sys.controller;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.LineCaptcha;
import com.jian.sys.common.ActiverUser;
import com.jian.sys.common.RandomValidateCodeUtil;
import com.jian.sys.common.ResultObj;
import com.jian.sys.common.WebUtils;
import com.jian.sys.pojo.Loginfo;
import com.jian.sys.service.LoginfoService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Date;

@RestController
@RequestMapping("/login")
public class LoginController {
    @Autowired
    LoginfoService loginfoService;
    /**
     * 登录验证
     * @param loginname
     * @param pwd
     * @return
     */
    @PostMapping("/login")
    public ResultObj login(@RequestParam("loginname")String loginname, @RequestParam("pwd")String pwd, @RequestParam("code")String code){
        Subject subject = SecurityUtils.getSubject();
        AuthenticationToken token = new UsernamePasswordToken(loginname,pwd);
//        String checkCode=WebUtils.getSession().getAttribute(RandomValidateCodeUtil.RANDOMCODEKEY).toString();
        String checkCode=WebUtils.getSession().getAttribute("code").toString();
        if(code.equals(checkCode)){
            try {
                subject.login(token);
                ActiverUser activerUser= (ActiverUser) subject.getPrincipal();
                WebUtils.getSession().setAttribute("user",activerUser.getUser());

                //将登录记录放到日志管理表
                Loginfo loginfo=new Loginfo();
                loginfo.setLoginname(activerUser.getUser().getRemark()+"-"+activerUser.getUser().getName());
                loginfo.setLoginip(WebUtils.getRequest().getRemoteAddr());
                loginfo.setLogintime(new Date());
                loginfoService.save(loginfo);

                return ResultObj.LOGIN_SUCCESS;
            }catch (AuthenticationException a){
                return ResultObj.LOGIN_ERROR_PASS;
            }
        }
        else
        {
            return ResultObj.LOGIN_ERROR_CODE;
        }
    }

    /**
     * 获取验证码 放在session中
     * @param response
     * @param session
     * @throws IOException
     */
    @RequestMapping("/getCode")
    public void getCode(HttpServletResponse response, HttpSession session) throws IOException {
        //定义图形验证码的长和宽
        LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(116, 36,4,5);
        session.setAttribute("code",lineCaptcha.getCode());
        ServletOutputStream outputStream=response.getOutputStream();
        ImageIO.write(lineCaptcha.getImage(),"JPEG",outputStream);
    }
//    /**
//     * 生成验证码
//     */
//    @RequestMapping(value = "/getVerify")
//    public void getVerify(HttpServletRequest request, HttpServletResponse response) {
//        try {
//            response.setContentType("image/jpeg");//设置相应类型,告诉浏览器输出的内容为图片
//            response.setHeader("Pragma", "No-cache");//设置响应头信息，告诉浏览器不要缓存此内容
//            response.setHeader("Cache-Control", "no-cache");
//            response.setDateHeader("Expire", 0);
//            RandomValidateCodeUtil randomValidateCode = new RandomValidateCodeUtil();
//            randomValidateCode.getRandcode(request, response);//输出验证码图片方法
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//    }
}
